#include "bucket.h"

template <int n>
void Bucket<n>::write(int position, std::fstream * fileStream)
{
    int offset = position * sizeBucket;

    fileStream->seekp(offset);

    fileStream->write((char*)&m_hashDigitCount, sizeof(int));

    for(int i = 0; i < n; i++)
        fileStream->write((char *)&(slots[i]), sizeArticle);

    fileStream->flush();
}

template <int n>
void Bucket<n>::read(int position, std::fstream * fileStream)
{
    int offset = position * sizeBucket;

    fileStream->seekg(offset);

    fileStream->read((char*)&m_hashDigitCount, sizeof(int));

    for(int i = 0;i < n; i++)
        fileStream->read((char *)&(slots[i]), sizeArticle);
}

template <int n>
bool Bucket<n>::addArticle(Article * article)
{
    bool success = false;

    for(int i = 0; i < n; i++)
    {	
        if((getArticle(i))->getNumber()== -1)
        {
            success = true;
            slots[i] = * article;
            break;
        }
    }

    return success;
}

template <int n>
ostream & operator<<(ostream &os, const Bucket<n> & bucket)
{
    os << "Bucket (" << n  << " slots) ( " << bucket.getHashDigitCount() << " hash digits)" << endl;

    for(int i = 0; i < n; i++)
    {
        Article article;
        article = bucket.slots[i];

        // nicht belegter Artikel (default-Konstruktor)
        if(article.getNumber() == -1)
            os << "--empty--" << endl;
        else
            os << article;
    }

    return os;
}
